package com.ybkj.daijia.server.controller.route;


import com.ybkj.daijia.common.Res;
import com.ybkj.daijia.server.service.route.IPointService;
import com.ybkj.daijia.server.utils.pinyinUtil;
import com.ybkj.daijia.server.zhuanxian.route.Point;
import java.util.Objects;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 起终点表 前端控制器
 * </p>
 *
 * @author xwy
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/route/point")
public class PointController {

    @Autowired
    private IPointService pointService;

    /**
     * 添加点（起/终点）
     * @param name 点名
     * @param province 点所属省
     * @param provinceAdcode 省区划代码
     * @param city 点所属市
     * @param cityAdcode 市区划代码
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Res<?> add(@RequestParam(value = "name")String name,
        @RequestParam(value = "province")String province,
        @RequestParam(value = "provinceAdcode")String provinceAdcode,
        @RequestParam(value = "city")String city,
        @RequestParam(value = "cityAdcode")String cityAdcode)
        throws BadHanyuPinyinOutputFormatCombination {

        //判断是否重复添加
        Point point = pointService.findOneBySame(name, province, city);
        if(Objects.isNull(point)){
            return Res.fail("该省市区下地名已存在，请重新添加");
        }
        //将地点名称转化为拼音
        String pinyin =  new pinyinUtil().toPinYin(name);

        return Res.ok();
    }

}
